const fs = require('fs'),
    path = require('path')
var times = 10

function sendHtml(res, fileName) {
    res.set({
        'content-type': 'text/html'
    })
    res.sendFile(fileName, {
        root: path.resolve(__dirname, '../view')
    })
}

//demo list
exports.list = function(req, res) {
    sendHtml(res, 'index.html')
}

//server sent event
exports.server_sent = function(req, res) {
    var fileName = 'server_sent_event.html'
    sendHtml(res, fileName)
}

exports.receive = function(req, res) {
    res.set({
        'content-type': 'text/event-stream',
        'cache-control': 'no-cache'
    })
    if (times > 0) {
        res.send(`:test\nretry:5000\nid:${times--}\nevent:ping\ndata:{"username": "blinkcat", "time": "${(new Date).toString()}"}\n\n`).end()
    } else {
        res.end()
    }
}

//websocket
exports.websocket = function(req, res) {
    sendHtml(res, 'websocket.html')
}

